<?php

$jobId = &$thisJobId;

/**
* fetch the detail
*
*/
$CLFetchers->fetchLstDataDetail($Db,
                                $jobId,
                                $SourceDb,
                                $nDetail,
                                $lstDetail);

if($isApprove){

  for($iRow = 0; $iRow < $nDetail; $iRow++){

    $source["modelEnt"][$iRow]  = $lstDetail["MODEL"][$iRow];
    $source["qtyEnt"][$iRow]    = $lstDetail["QTY"][$iRow];
    $source["headEnt"][$iRow]   = $lstDetail["HEAD"][$iRow];
    $source["lengthEnt"][$iRow] = $lstDetail["LENGTH"][$iRow];
    $source["serNumEnt"][$iRow] = $lstDetail["SER_NUM"][$iRow];

    $CFetchers->fetchModelId($Db,
                             $source["modelEnt"][$iRow],
                             $SourceDb->fetchValue("srcDbModel"),
                             $modelId);

    $CFetchers->fetchIsSerNumber($Db,
                                 $source["modelEnt"][$iRow],
                                 $modelId,
                                 $SourceDb->fetchValue("srcDbModel"),
                                 $isSerNumber);

    if($isSerNumber){

      $SN   = $CLocal->procSN($source,
                              $iRow);

      $szSn = sizeof($SN);

      for($iSn = 0; $iSn < $szSn; $iSn++){

        $values = " '".$jobId."', ".      /* JobID      */
                  "  ".($iRow+1)."  , ".  /* Detail No  */
                  "  ".($iSn+1)."   , ".  /* Sub Dtl No */
                  " '".$SN[$iSn]."',  ".  /* Serial Number */
                  " ' ', ".               /* Warehouse  */
                  "  "._PEMBELIAN." ";    /* TX_TYPE    */

        $CHandlers->insToSN($Db,
                            $values,
                            $SourceDb->fetchValue("srcDbSN"));
      }
    }else{

      /* Do nothing */
    }
  }
}else{

  /**
   * Check out for each SN
   * make sure it is safe to cancel
   */
  for($iRow = 0; $iRow < $nDetail; $iRow++){

    $source["modelEnt"][$iRow]  = $lstDetail["MODEL"][$iRow];
    $source["qtyEnt"][$iRow]    = $lstDetail["QTY"][$iRow];
    $source["headEnt"][$iRow]   = $lstDetail["HEAD"][$iRow];
    $source["lengthEnt"][$iRow] = $lstDetail["LENGTH"][$iRow];
    $source["serNumEnt"][$iRow] = $lstDetail["SER_NUM"][$iRow];

    $CFetchers->fetchModelId($Db,
                             $_POST["modelEnt"][$iRow],
                             $SourceDb->fetchValue("srcDbModel"),
                             $modelId);

    $CFetchers->fetchIsSerNumber($Db,
                                 $_POST["modelEnt"][$iRow],
                                 $modelId,
                                 $SourceDb->fetchValue("srcDbModel"),
                                 $isSerNumber);

    if($isSerNumber){

      $SN   = $CLocal->procSN($source,
                              $iRow);

      $szSn = sizeof($SN);

      for($iSn = 0; $iSn < $szSn; $iSn++){

        $CFetchers->fetchIsSnClean($Db,
                                   $SN[$iSn],
                                   _PEMBELIAN,
                                   $SourceDb->fetchValue("srcDbSN"),
                                   $isClean);

        if(!$isClean){

          $Misc->usrDieJs("Error: SN is not clean or being used, ".$SN[$iSn]." ");
        }
      }
    }else{

      /* Do nothing */
    }
  }

  /**
   * if everything is ok
   * then drop all entried SN
   */
  $CHandlers->dbEntDelete($Db,
                          $jobId,
                          $SourceDb->fetchValue("srcDbSN"));
}
?>